var docDetailsDialog;
var searchPageLayout;
var mapUserDialog;
var userDialog;
function createLayoutForSearch()
{
	searchPageLayout = $("#contentDiv").layout({
	     south__paneSelector:	"#searchOptionSide"
		,center__paneSelector:	"#searchResultSide"
		,spacing_open: 			5 
		,spacing_closed: 		5
		,resizable: 			false
		,south__size:			220
		
	});
	loadOnChangeMethods();
	loadResultTable();
    userDialog = $("#userSearchDialogue").dialog({
   		height: 350,
  		width: 750,
  		modal: true,
  		zIndex: 8999,
  		autoOpen: false
   	});
	docDetailsDialog = $("#docDtlsDialogue").dialog({
			
			height: 500,
			width: 950,
			modal: true,
			autoOpen: false,
			zIndex: 8888,
			dialogClass : 'docDtlsDialog'
			
			
		});
	mapUserDialog = $("#mapUsers").dialog({
		
		height: 500,
		width: 950,
		modal: true,
		autoOpen: false,
		zIndex: 8888,
		dialogClass : 'docDtlsDialog'
		
		
	});
	var numOfDialog = $(".docDtlsDialog").length;
	if(numOfDialog > 1)
		$(".docDtlsDialog").first().empty().remove();
}


function accessDoc(docID)
{
	var ops = $("#opsValue").val();
	
	
	var url="accessDocDtls.htm";
	$.post(url,{ops:ops,docID:docID},function(data){
		$("#docDtlsDialogue").empty().html(data);		 
		$("#docDtlsDialogue").dialog("open");		
	},"html");
}
function loadOnChangeMethods()
{
	$("#docTypeID").change(function(){
	
		var docType = $("#docTypeID").val();
		if($(this).val()!=0)
		{
			$.getJSON("fetchCategory.htm",{id: $(this).val(), ajax: 'true',parentlevel: docType}, function(j){
				 var options = '';
				 if(j!=null)
				 {
				      for (var i = 0; i < j.length; i++) {
				        options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
				      }
				      $("#docCatID").html(options);
				 }
			});
		}
		else{
			$("#docCatID").empty();
		}
	
	});
	$("#docCatID").change(function(){
		
		var docCat = $("#docCatID").val();
		if($(this).val()!=0)
		{
			$.getJSON("fetchSubCategory.htm",{id: $(this).val(), ajax: 'true',parentlevel: docCat}, function(j){
				 var options = '';
				 if(j!=null)
				 {
				      for (var i = 0; i < j.length; i++) {
				        options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
				      }
				      $("#docSubCatID").html(options);
				 }
		    });	
		}
		else{
			$("#docSubCatID").empty();
		}
	});
}


function searchDocForOps(ops){
	
	
	var formData = $("#searchForm").formSerialize();
	

formData+="&page=1&rows=10"; 	
	jQuery("#searchResults").jqGrid('setGridParam',{url:"searchDoc.htm",page:1,postData:formData}); 
	jQuery("#searchResults").jqGrid('setCaption',"Search Results") .trigger('reloadGrid');

	/*$.getJSON("searchDoc.htm",formData,function(data){
		var options ='';
		if(data!=null)
		{
			
		for (var i = 0; i < data.length; i++) {
			
		    	  options += '<tr><td align="center"><a  href="javascript:accessDoc('+"'" +data[i].docId+"'"+','+ops+');">'+ data[i].docId + '</a></td><td align="left">' + data[i].docName + '</td></tr>';
	      }
	
			 $("#searchResultBody").html(options);
		}
		else
			alert("No Matching Documents found");
		      //alert(options);
	    },"html");
	*/
	
}
function loadResultTable()
{
	var opsValue = $("#opsValue").val();
	
	$("#searchResults").jqGrid(
			{ url:'searchDoc.htm', datatype: "json", 
				mtype: 'GET',
				colNames:['Document Id','Document Name','Map User'], 
				colModel:[ {name:'docCodeURL',index:'id'}, 
				           {name:'docName',index:'name', width:100,sortable:false,editable:true}, 
				           {name:'update',index:'update', width:75,sortable:false}
				           ], 
		           jsonReader : {
		               root: "rows",
		               page: "page",
		               total: "total",
		               records: "records",  
		               repeatitems: false			            
		         },
				 rowNum:10, 
				 rowList:[10,20,30], 
				 pager: '#pager2', 
				 height:170,
				 viewrecords: true,			 
				 multiselect: false, 
				 caption: "Search Results",				 	      
				 autowidth: true,		
				 onSelectRow: function(ids) 
					{
						 accessDoc(ids,opsValue);
					} 
			}
			); 
}

function updateAtt(docCode)
{	
	alert(docCode);
	mapUserDialog.dialog("open");
}
function openSearchUser()
{
	
	$("#userSearchDialogue").dialog("open");
}
function moveToViewGroup(){
    $("#userList > li.ui-selected").each(function(index){
            var liId = $(this).attr('id');
            var liText = $(this).html();
            $(this).remove();
            $("#viewGroupList").append("<li id='"+ liId +"' class='ui-state-default'>"+ liText +"</li>");
            $("#viewGroupDiv").append("<input type='hidden' name='viewGroup' value='"+ liId +"'>");
        });
    //$("#viewGroupList").selectable("refresh");
   
}
function moveToPrintGroup(){
    $("#userList > li.ui-selected").each(function(index){
            var liId = $(this).attr('id');
            var liText = $(this).html();
            $(this).remove();
            $("#printGroupList").append("<li id='"+ liId +"' class='ui-state-default'>"+ liText +"</li>");
            $("#printGroupDiv").append("<input type='hidden' name='printGroup' value='"+ liId +"'>");
        });
    //$("#viewGroupList").selectable("refresh");
   
}